import binaryTree.TreeNode;

import java.util.ArrayList;
import java.util.List;

class Class257 {
    List<String> list = new ArrayList<String>();
    public List<String> binaryTreePaths(TreeNode root) {
        if (root == null) {
            return null;
        }
        dfs(root, new StringBuilder());
        return list;
    }

    public void dfs(TreeNode root, StringBuilder sb) {
        if (root == null) {
            return;
        }
        sb.append(root.val+"->");
        if (root.left == null && root.right == null) {
            sb.delete(sb.length()-2, sb.length());
            list.add(sb.toString());
            sb.append("->");
        }
//        if (root.left == null && root.right == null) {
//
//            list.add(sb.toString());
//        }

        dfs(root.left, sb);
        dfs(root.right, sb);
        sb.delete(sb.length()-3, sb.length());  //回溯算法了
    }


    public static void main(String[] args) {
        Class257 class257 = new Class257();
        TreeNode treeNode1 = new TreeNode(1);
        TreeNode treeNode2 = new TreeNode(2);
        TreeNode treeNode3 = new TreeNode(3);
        TreeNode treeNode4 = new TreeNode(5);
        treeNode1.left=treeNode2;
        treeNode1.right=treeNode3;
        treeNode2.right=treeNode4;
        List<String> strings = class257.binaryTreePaths(treeNode1);
        for (String a:
            strings ) {
            System.out.println(a);
        }


    }

}
